import fetch from '@system.fetch';
import prompt from '@system.prompt';

const host = "https://builddfamilyream.cn";
function commRequest(url, method, params) {
    return new Promise((resolve, reject) => {
      fetch.fetch({
        url: host + url, // 目标服务器url
        data: params,
        method: method,
        header:{
          'Content-Type':  'application/json',
          "Token":"06382529f1e54a908fcf50e97d6f582d"
        },
        success:function(res){
          let data=JSON.parse(res.data);
          if(data.code === 200){
            resolve(data)
          }else if(data.code === 500){
            prompt.showToast({
              message: data.message,
              duration: 2000
            })
            reject(res)
          }else{
            prompt.showToast({
              message: data.message,
              duration: 2000
            })
            reject(res)
          }
        },
        fail:function(res){
          prompt.showToast({
            message: '网络错误...',
            duration: 2000
          })
          reject(res)
        }
      });
    })
  }

  //POST请求
function post(url, params) {
    return commRequest(url, "POST", params)
  }
  
  //GET请求
  function get(url, params) {
    return commRequest(url, "GET", params)
  }
  
  export default {
    post,
    get,
  }